package com.jzr.medical.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.util.FileUtil;
import com.jzr.common.util.U;
import com.jzr.medical.common.LayServiceImpl;
import com.jzr.medical.db1.mapper.TMediaMapper;
import com.jzr.medical.db1.model.TMedia;
import com.jzr.medical.db1.service.MediaService;
import com.jzr.medical.db1.vo.MediaVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

@Service
public class MediaServiceImpl extends LayServiceImpl<TMediaMapper,TMedia,MediaVo,Integer> implements MediaService {
    @Value("${file.media-upload-folder}")
    private String uploadFolder;

    @Override
    public boolean removeById(Serializable id) {
        String fileName = getFileName((Integer)id);
        if(U.isNotBlank(fileName)) {
            File dest = new File(uploadFolder, fileName);
            if (dest.exists()) {
                dest.delete();
            }
        }
        return super.removeById(id);
    }

    @Override
    public int delete(MediaVo record) {
        if(record == null || record.getId() == null){
            return 0;
        }
        String fileName = getFileName(record.getId());
        if(U.isNotBlank(fileName)) {
            File dest = new File(uploadFolder, fileName);
            if (dest.exists()) {
                dest.delete();
            }
        }
        baseMapper.deleteById(record.getId());
        return 0;
    }

    @Override
    public MediaVo update(MediaVo record) {
        if(record == null){
            return null;
        }
        if(record.getId() == null){
            return saveAndGet(record);
        }
        //修改title
        try {
            if (U.isNotBlank(record.getTitle())) {
                TMedia old = baseMapper.selectById(record.getId());
                String fileName = old.getFileName();
                if (U.isNotBlank(fileName) && "html".equalsIgnoreCase(FileUtil.getExtension(fileName))) {
                    File dest = new File(uploadFolder, fileName);
                    if (dest.exists()) {
                        String sb = FileUtil.readAll(dest);
                        sb = sb.replaceAll("<title>(\\w|\\W)*</title>","<title>"+record.getTitle()+"</title>");
                        FileUtil.write(dest,sb.toString());
                    }
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        int count = baseMapper.updateById(record);
        if(count <= 0){
            return null;
        }
        return this.getByIdV(record.getId());
    }


    @Override
    public Integer updateFilename(Integer id, String fileName) {
        TMedia media = new TMedia();
        media.setId(id);
        media.setFileName(fileName);
        return baseMapper.updateById(media);
    }

    @Override
    public String getFileName(Integer id) {
        QueryWrapper<TMedia> qw = new QueryWrapper<>();
        qw.select("file_name");
        qw.eq("id",id);
        TMedia m = baseMapper.selectOne(qw);
        return m == null ? "" : m.getFileName();
    }

}
